import store from '../vuex/store'
import { share, getBrowser } from '../utils/utils'
import { weixinApi } from '../api/api'
import wx from 'weixin-js-sdk'
import { allApi } from '../api/api'

const setShare = ()=>{
    let protocol = location.protocol
    let host = location.host
    let pathname = location.pathname
    let search = location.search

    let realUrl = `${protocol}//${host}${pathname}${search}#share-common`

    store.dispatch('setShare',{
        message:{
            title: '安牛有借，海量客户，一键拓客', // 分享标题
            desc: `为您提供拓客、数据、电子合同、客户管理等saas工具。`, // 分享描述
            link: realUrl, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'https://static.kongapi.com/pic/loading_1.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {

            },
            cancel: function () {

            }
        },
        timeline:{
            title: '安牛有借，海量客户，一键拓客', // 分享标题
            link: realUrl, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'https://static.kongapi.com/pic/loading_1.png', // 分享图标
            success: function () {

            },
            cancel: function () {

            }
        },
        qq:{
            title: '安牛有借，海量客户，一键拓客', // 分享标题
            desc: `为您提供拓客、数据、电子合同、客户管理等saas工具。`, // 分享描述
            link: realUrl, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'https://static.kongapi.com/pic/loading_1.png', // 分享图标
            success: function () {

            },
            cancel: function () {

            }
        },
    })
}

export default class RouterConfig {
    constructor(router) {
        this.router = router
    }
    render() {
        const router = this.router
        router.beforeEach((to, from, next) => {
            document.title = to.meta.title

            window.localStorage.setItem('timer', new Date().getTime())
            window.localStorage.setItem('currentUrl', from.path)
            window.localStorage.setItem('currentTitle', from.meta.title)

            window.localStorage.setItem('beforeTitle', to.meta.title)
            setShare()

            wx.ready(function(res){
                setShare()
            });

            let routerList = store.state.localInfo.routerList
            let isRouterFirst = routerList.length == 0
            let toIndex = _.indexOf(routerList,to.name)
            let fromIndex = _.indexOf(routerList,from.name)
            // console.log(toIndex)
            // console.log(fromIndex)
            if(toIndex == -1 && fromIndex == -1){
                store.dispatch('setRouterAnimation','router')
                store.dispatch('setRouterList',{
                    name:to.name
                })
                next()
                return
            }
            if(toIndex == -1 || toIndex > fromIndex){
                store.dispatch('setRouterAnimation','router-in')
                store.dispatch('setRouterList',{
                    name:to.name
                })
            }
            if(toIndex != -1 && toIndex == fromIndex){
                store.dispatch('setRouterAnimation','router-in')
            }
            if(toIndex != -1 && toIndex < fromIndex){
                store.dispatch('setRouterAnimation','router-out')
                store.dispatch('setRouterList',{
                    name:to.name,
                    index:fromIndex
                })
            }
            // let isRouterLast = to.name == routerList[routerList.length-2]

            // if(from.name == undefined){
            //     store.dispatch('setRouterList',to.name)
            //     next()
            //     return
            // }

            // if(isRouterFirst || routerList.length > 1 && isRouterLast){
            //     if(isRouterFirst){
            //         store.dispatch('setRouterAnimation','router-in')
            //         store.dispatch('setRouterList',to.name)
            //     }
            //     else{
            //         store.dispatch('setRouterAnimation','router-out')
            //         store.dispatch('setRouterList','removeLastRouter')
            //     }
            // }
            // else{
            //     store.dispatch('setRouterAnimation','router-in')
            //     store.dispatch('setRouterList',to.name)
            // }
            // 大数据埋点
            allApi.onEventLogger({
                currentPageUrl: window.location.href,
                currentPageName: document.title,
                refererPageUrl: document.referrer + window.localStorage.getItem('currentUrl'),
                refererPageName: window.localStorage.getItem('currentTitle'),
                refererPageStamp: new Date().getTime() - window.localStorage.getItem('timer'),
                eventType:'track_view',
                eventName:'page_v',
                eventDescr:'页面pv',
                eventParamsValue:JSON.stringify({
                    title:document.title
                }),
            })
            next()

        })
    }
}
